home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / packer / lzw14 / test_lzw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-12  |  3.9 KB  |  136 lines

  1. /*
  2. **   TEST_LZW.C       Copyright (C) 1992 by MarshallSoft Computing, Inc.
  3. **
  4. **   This program is used to compress, expand, and verify each specified
  5. **   file. It's purpose is for you to test the LZW4C library on your own
  6. **   files. Your files are never modified. However, you should NOT have a
  7. **   file named "XXX.XXX" or "YYY.YYY".  Compression ratios are printed
  8. **   for each file compressed. For example, to compress all files ending
  9. **   in *.C in your current directory, type:
  10. **
  11. **        TEST_LZW *.c
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <dos.h>
  16. #include <fcntl.h>
  17. #include <sys\types.h>
  18. #include <sys\stat.h>
  19. #include <io.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <conio.h>
  24.  
  25. #include "RW_IO.H"
  26. #include "LZW4C.H"
  27. #include "DIR_IO.H"
  28. #include "SAYERROR.H"
  29.  
  30. FILE *FilePtr1;
  31. FILE *FilePtr2;
  32.  
  33. void main(int argc,char *argv[])
  34. {int i, k;            /* loop counters */
  35.  int x, y;            /* last bytes read during compare */
  36.  int RetCode;         /* return code */
  37.  float Ratio;         /* compression ratio */
  38.  long Index;          /* byte count */
  39.  char Filename[15];   /* next filename */
  40.  int Files = 0;       /* # files compressed/expanded */
  41.  int BitCode;         /* 12, 13, or 14 */
  42.  /* begin */
  43.  switch(argc)
  44.    {case 2:
  45.       /* preferred value = 14 bits */
  46.       BitCode = 14;
  47.       break;
  48.     case 3:
  49.       /* get bit code value from command line */
  50.       BitCode = atoi(argv[2]);
  51.       break;
  52.     default:
  53.       printf("Usage: TEST_LZW <filespec> {<bitcode>}\n");
  54.       exit(1);
  55.    }
  56.  /* use 9 to <BitCode> bit codes */
  57.  RetCode = InitLZW(malloc,BitCode);
  58.  if(RetCode<0)
  59.    {SayError(RetCode);
  60.     exit(2);
  61.    }
  62.  /* flush the keyboard */
  63.  puts("\nTEST_LZW 1.1: Type any key to abort...");
  64.  for(i=0;;i++)
  65.    {if(kbhit())
  66.       {puts("\n...Aborted by user !");
  67.        break;
  68.       }
  69.     /* get next filename */
  70.     if(i==0) RetCode = FindFirst(argv[1],Filename);
  71.     else RetCode = FindNext(Filename);
  72.     if(!RetCode) break;
  73.     /* force to uppercase */
  74.     for(k=0;k<strlen(Filename);k++) Filename[i] = toupper(Filename[i]);
  75.     /* skip 'work' files XXX.XXX and YYY.YYY */
  76.     if( (strcmp(Filename,"XXX.XXX")==0) ||
  77.         (strcmp(Filename,"YYY.YYY")==0) ) continue;
  78.     /* open input file for compression */
  79.     if(!ReaderOpen(Filename)) exit(3);
  80.     /* open output file for compression */
  81.     if(!WriterOpen("XXX.XXX")) exit(4);
  82.     /* do the compression */
  83.     Files++;
  84.     printf("\nCompressing %12s ",Filename);
  85.     if((RetCode=Compress(Reader,Writer))<0)
  86.       {SayError(RetCode);
  87.        exit(5);
  88.       }
  89.     /* report compression ratio */
  90.     if(ReaderCount() > 0)
  91.        {Ratio = (float)(WriterCount())/(float)ReaderCount();
  92.         printf(" OK (%0.2f)\n",Ratio);
  93.        }
  94.     else puts("???");
  95.     /* close files */
  96.     ReaderClose();
  97.     WriterClose();
  98.     /* open input file for expansion */
  99.     if(!ReaderOpen("XXX.XXX")) exit(6);
  100.     /* open output file for expansion */
  101.     if(!WriterOpen("YYY.YYY")) exit(7);
  102.     /* do the expansion */
  103.     printf("  Expanding %12s ",Filename);
  104.     if((RetCode=Expand(Reader,Writer))<0)
  105.       {printf("Expand returns error %d\n",RetCode);
  106.       }
  107.     /* close files */
  108.     ReaderClose();
  109.     WriterClose();
  110.     printf(" OK\n");
  111.     /* compare original to expanded file */
  112.     FilePtr1 = fopen(Filename,"rb");
  113.     FilePtr2 = fopen("YYY.YYY","rb");
  114.     printf("  Comparing              ");
  115.     Index = 0;
  116.     while(1)
  117.       {x = fgetc(FilePtr1);
  118.        y = fgetc(FilePtr2);
  119.        Index++;
  120.        /* print dot every 4K bytes */
  121.        if((Index&0x0fff)==0) putchar('.');
  122.        if((x==EOF)&&(y==EOF)) break;
  123.        if(x!=y)
  124.          {printf("ERROR: Difference between files at index %ld\n",Index-1);
  125.           exit(8);
  126.          }
  127.       }
  128.     printf(" OK\n");
  129.     fclose(FilePtr1);
  130.     fclose(FilePtr2);
  131.    }
  132.  /* all done */
  133.  TermLZW(free);
  134.  printf("\n%d files tested\n",Files);
  135.  exit(0);
  136. }